package com.bangcommunity.bbframe.springredis.mq;

import com.bangcommunity.bbframe.common.base.MainConfigLoader;
import com.bangcommunity.bbframe.springredis.RedisClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RedisMqProducerBuilder {

    private static Logger logger = LoggerFactory.getLogger(RedisMqProducerBuilder.class);

    private static class MqBuilderHolder {
        private static RedisMqProducerBuilder instance = new RedisMqProducerBuilder();
    }

    private RedisMqProducerBuilder() {
    }

    public static RedisMqProducerBuilder getInstance() {
        return MqBuilderHolder.instance;
    }

    public <T> void createRedisTask(T task, String key) {
        String taskKey = getRedisKey(key);
        try {
            logger.debug("create Job, task:{},taskkey:{},key:{}", task, taskKey,key);
            RedisClient.lPush(taskKey,task);
        } catch (Exception e) {
            logger.error("create Job exception, task:{},taskKey{}", task, taskKey, e);
        }
    }

    private String getRedisKey(String taskKey) {

        return new StringBuilder().append(MainConfigLoader.getInstance().getClientCode()).append(":").append(taskKey)
                .toString();
    }
}
